<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Tools\ApiService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class BaseController extends Controller
{
    protected $userArr;
    protected $requestArr;
    protected $requestFile;

    public function __construct(Request $request)
    {
        # 获取POST数据
        $this->requestArr = $request->input();
        # 获取file
        $this->requestFile = $request->file();
        //        $this->userArr['store_id'] = 80;

        # 传输方式
        /*if (!$request->isMethod('post')) {
            errorMsg('传输方式错误！');
        }*/
        # 时间戳判断
        $timestamp = $request->header('timestamp');
        /*if (!$timestamp || abs($timestamp - time()) > 600) {
            errorMsg('时间戳错误');
        }*/
        # 签名验证
        $sign = $request->header('sign');
        if (!ApiService::appSignVerify($sign, $timestamp)) {
            errorMsg('签名验证失败', 1001);
        }
        # App登录信息判断
        $token = $request->header('token');
        if (!$token) {
            errorMsg('登录信息错误', 1001);
        }
        $tokenStr = Redis::get("XCX_TOKEN:{$token}");
        if ($tokenStr) {
            Redis::EXPIRE("XCX_TOKEN:{$token}", 86400 * 30);
            $this->userArr = json_decode($tokenStr, true);
        } else {
            errorMsg('登录信息错误', 1001);
        }
    }
}
